'''
Contains the Srmanager class.

Created on 3 dec 2011

@author: Adam
'''

from managers import CharacterManager, ScriptManager

class Srpy(object):
    '''
    The main class for the python side of the sunray engine.
    This should be the only class the c# side needs to have contact with.
    '''
    
    def __init__(self, game, managerlist = None, *managers):
        '''
        Constructor
        '''
        self._game = game
        self.managers = {}
        
        #Loop all manager names and try to create an instance of them.
        if  managers:
            for m in managers:
                m = m.replace('\n', '')
                try:
                    self.managers[m] = eval(("%s(self._game)" % m))
                except:
                    print "There was an error when trying to load the %s manager" % m
             
        if managerlist:   
            for m in managerlist:
                m = m.replace('\n', '')
                try:
                    self.managers[m] = eval(("%s(self._game)" % m))
                except:
                    print "There was an error when trying to load the %s manager" % m

        self.submanagers()
                
    def delmanager(self, name):
        if not self.managers.keys.Contains(name):
            return False    
        self._game.Components.Remove(self.managers[name])
        del self.managers[name]
        return True
        
    def addmanager(self, name):
        """Try to add a manager to the games component list."""
        try:
            m = eval("%s(self._game)" % name)
        except:
            print "There was an error when trying to add the %s manager" % m
            return False
            
        if self.managers.keys().Contains(m):
            print "Manager already active."
            return False
        else:
            self.managers[name] = m
            return True
                
    def submanagers(self):
        """Loop managers and subscribe them to the games Components list."""
        for m in self.managers.values():
            #If the manager is not already on the list we add it.
            if not self._game.Components.Contains(m):
                self._game.Components.Add(m)
                
    def __getitem__(self, name):
        return self.managers[name]
        